package com.umeng.soexample.mvp.view.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.AddAddressEntity;
import com.umeng.soexample.bean.AddressListEntity;
import com.umeng.soexample.bean.DelAddressEntity;
import com.umeng.soexample.dao.USerService;
import com.umeng.soexample.di.compont.DaggerAddAddressFragmentCompont;
import com.umeng.soexample.di.modules.AddressListFragmentModules;
import com.umeng.soexample.mvc.control.SPHelperSave;
import com.umeng.soexample.mvc.view.AddresActivity;
import com.umeng.soexample.mvp.contract.AddressListFragmentContract;
import com.umeng.soexample.mvp.persenter.AddressListFragmentPersenter;
import com.umeng.soexample.tbarea.area.BottomDialog;


public class AddAddressFragment extends BaseFragment<AddressListFragmentPersenter>  implements AddressListFragmentContract.AddressListFragmentIView, View.OnClickListener {
    private View view;
    private String user_id;
    private String address_id;
    private TextView tv_add_address_title;
    private EditText et_add_address_user_name;
    private EditText et_add_address_phone;
    private EditText et_add_address_area;
    private EditText et_add_address_address_detail;
    private RadioGroup rg_add_address;
    private String address_tag = "";
    private CheckBox cb_add_address_address_default;
    private String address_default = "";
    private Button btn_add_address_save;
    private String user_name;
    private String phone;
    private String area;
    private String address_detail;
    private ViewDataBinding mBinding;


    @Override
    public View createView(LayoutInflater inflater) {
        view=inflater.inflate(R.layout.fragment_add_address,null);
        return view;
    }



    @Override
    public void init(View view) {
        tv_add_address_title = view.findViewById(R.id.tv_add_address_title);
        et_add_address_user_name = view.findViewById(R.id.et_add_address_user_name);
        et_add_address_phone = view.findViewById(R.id.et_add_address_phone);
        et_add_address_area = view.findViewById(R.id.et_add_address_area);
        et_add_address_address_detail = view.findViewById(R.id.et_add_address_address_detail);
        rg_add_address = view.findViewById(R.id.rg_add_address);
        cb_add_address_address_default = view.findViewById(R.id.cb_add_address_address_default);
        btn_add_address_save = view.findViewById(R.id.btn_add_address_save);

        if (TextUtils.isEmpty(address_id)){
            tv_add_address_title.setText("添加收货地址");
        }else{
            tv_add_address_title.setText("编辑收货地址");
        }
    }

    @Override
    public void initData() {
        Log.e("sh","initData"+user_id);
        p.addressListRequest(user_id);
    }

    @Override
    public void setupDagger() {
        DaggerAddAddressFragmentCompont.builder().addressListFragmentModules(new AddressListFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        view.findViewById(R.id.iv_add_address_back).setOnClickListener(this);
        btn_add_address_save.setOnClickListener(this);
        /**
         * 标签选择
         */

        rg_add_address.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_add_address_address_tag_jia:
                        address_tag="家";
                        break;
                    case R.id.rb_add_address_address_tag_gongsi:
                        address_tag="公司";
                        break;
                    case R.id.rb_add_address_address_tag_xuexiao:
                        address_tag="学校";
                        break;

                    case R.id.rb_add_address_address_tag_qita:
                        address_tag="其他";
                        break;
                }
            }
        });


        /**
         * 收货人
         */

        et_add_address_user_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0 && et_add_address_phone.getText().toString().length() > 0 && et_add_address_area.getText().toString().length() > 0 && et_add_address_address_detail.getText().toString().length() > 0){
                    btn_add_address_save.setBackgroundColor(Color.parseColor("#444444"));
                }else{
                    btn_add_address_save.setBackgroundColor(Color.parseColor("#B0B0B0"));
                }
            }
        });

        /**
         * 联系电话
         */

        et_add_address_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0 && et_add_address_user_name.getText().toString().length() > 0 && et_add_address_area.getText().toString().length() > 0 && et_add_address_address_detail.getText().toString().length() > 0 ){
                    btn_add_address_save.setBackgroundColor(Color.parseColor("#444444"));
                }else{
                    btn_add_address_save.setBackgroundColor(Color.parseColor("#B0B0B0"));
                }
            }
        });


        /**
         * 所在地区
         */
//        et_add_address_area.setOnClickListener(new View.OnClickListener() {
//                                                   @Override
//                                                   public void onClick(View v) {
////                Intent intent = new Intent(getActivity(), AddresActivity.class);
////                startActivity(intent);
//                                                       BottomDialog dialog = new BottomDialog(getContext());
//                                                       dialog.setResultCallBack(result -> et_add_address_area.setText(String.format("地区：%s", result)));
//                                                       //et_add_address_area.setOnClickListener(v -> dialog.show());
//                                                   }
        et_add_address_area.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add_address();
            }
            private void add_address() {
                BottomDialog dialog = new BottomDialog(getContext());
                dialog.setResultCallBack(new BottomDialog.OnSelectedResultCallBack() {
                    @Override
                    public void onResult(String result) {
                        et_add_address_area.setText(result+"");
                    }
                });
                dialog.show();
            }
        });


//        private void add_address() {
//            BottomDialog dialog = new BottomDialog(getContext());
////        dialog.setResultCallBack(result -> mBinding.tv.setText(String.format("地区：%s", result)));
////        mBinding.ll_add_address_area.setOnClickListener(v -> dialog.show());
//            dialog.setResultCallBack(new BottomDialog.OnSelectedResultCallBack() {
//                @Override
//                public void onResult(String result) {
////                Toast.makeText(getContext(), ""+result, Toast.LENGTH_SHORT).show();
//                    et_add_address_area.setText(result + "");
//                }
//            });
//
//            dialog.show();
//        }



//
//        et_add_address_area.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (s.length() > 0 && et_add_address_phone.getText().toString().length() > 0 && et_add_address_user_name.getText().toString().length() > 0 && et_add_address_address_detail.getText().toString().length() > 0){
//                    btn_add_address_save.setBackgroundColor(Color.parseColor("#444444"));
//                }else{
//                    btn_add_address_save.setBackgroundColor(Color.parseColor("#B0B0B0"));
//                }
//            }
//        });



        /**
         * 详细地址
         */


        et_add_address_address_detail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0 && et_add_address_phone.getText().toString().length() > 0 && et_add_address_area.getText().toString().length() > 0 && et_add_address_user_name.getText().toString().length() > 0){
                    btn_add_address_save.setBackgroundColor(Color.parseColor("#444444"));
                }else {
                    btn_add_address_save.setBackgroundColor(Color.parseColor("#B0B0B0"));
                }
            }
        });

    }

    @Override
    public void addAddressSuccess(AddAddressEntity entity) {
        if ("1".equals(entity.statues)){
            getActivity().finish();
        }
    }

    @Override
    public void addressListSuccess(AddressListEntity entity) {
        Log.e("sh","addressListSuccess" + 1);
        
        if (! TextUtils.isEmpty(address_id)){
            Log.e("sh","addressListSuccess" + 1);

            for (AddressListEntity.ValuesBean valuesBean: entity.values
                 ) {
                if (address_id.equals(valuesBean.address_id)){
                    Log.e("sh","addressListSuccess: "+2);
                    et_add_address_user_name.setText(valuesBean.user_name);
                    et_add_address_phone.setText(valuesBean.phone);
                    et_add_address_area.setText(valuesBean.address_area);
                    et_add_address_address_detail.setText(valuesBean.address_detail);

                    if ("家".equals(valuesBean.address_tag)){
                        rg_add_address.check(R.id.rb_add_address_address_tag_jia);
                        address_tag="家";
                    }else if ("公司".equals(valuesBean.address_tag)){
                        rg_add_address.check(R.id.rb_add_address_address_tag_gongsi);
                        address_tag="公司";
                    }else if ("学校".equals(valuesBean.address_tag)){
                        rg_add_address.check(R.id.rb_add_address_address_tag_xuexiao);
                        address_tag="学校";
                    }else if ("其他".equals(valuesBean.address_tag)){
                        rg_add_address.check(R.id.rb_add_address_address_tag_qita);
                        address_tag="其他";
                    }

                    if ("1".equals(valuesBean.address_default)){
                        cb_add_address_address_default.setChecked(true);
                    }else{
                        cb_add_address_address_default.setChecked(false);
                    }

                    break;
                }
            }
        }
    }

    @Override
    public void delAddressSuccess(DelAddressEntity entity) {
        if ("1".equals(entity.statues)){
            p.addAddressRequest(user_id, user_name, phone, area, address_detail, address_tag);
        }
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //返回上一页
            case R.id.iv_add_address_back:
                getActivity().finish();
                break;
                
                //保存
            case R.id.btn_add_address_save:   
                save();
                break;
        }
    }

    private void save() {
        user_name = et_add_address_user_name.getText().toString().trim();
        phone = et_add_address_phone.getText().toString().trim();
        area = et_add_address_area.getText().toString().trim();
        address_detail = et_add_address_address_detail.getText().toString().trim();
        if (TextUtils.isEmpty(user_name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(area) || TextUtils.isEmpty(address_detail)){
            return;
        }
        
        if (phone.length()<11){
            Toast.makeText(getContext(), "请你填写正确11位手机号码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (cb_add_address_address_default.isChecked()){
            address_default="1";
        }else{
            address_default="0";
        }

        if (TextUtils.isEmpty(address_id)){
            p.addAddressRequest(user_id, user_name, phone, area, address_detail, address_tag);
        }else{
            p.delAddressRequest(address_id);
            if (cb_add_address_address_default.isChecked()){
                SPHelperSave.getInstance().setAddress_default(address_id);
            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Cursor cursor_user = USerService.getInstance().query("user", null, null);
        if (cursor_user.moveToNext()){
             user_id = cursor_user.getString(cursor_user.getColumnIndex("user_id"));
        }else{
            Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
        }

        Bundle arguments = getArguments();
        address_id=arguments.getString("address_id");
    }
}
